package org.tong.jackson;

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.function.Function;

/**
 * @author
 * @date 2022-10-09 10:10
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveSolve.class)
public @interface Sensitive {

    Strategy strategy() default Strategy.PHONE;

    @Getter
    enum Strategy {
        NONE((arg) -> arg), PHONE((phone) -> {
            return phone.replaceAll("(\\d{4})\\d{3}(\\d{4})", "$1***$2");
        });

        Strategy(Function<String, String> mapper) {
            this.mapper = mapper;
        }

        private Function<String, String> mapper;
    }
}
